//
//  SegView.m
//  体育商城
//
//  Created by  on 2022/9/23.
//

#import "SegView.h"
@interface SegView()
@property (strong, nonatomic) IBOutlet UIView *line;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter1;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter2;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter3;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter4;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter5;
@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet UIButton *btn3;
@property (strong, nonatomic) IBOutlet UIButton *btn4;
@property (strong, nonatomic) IBOutlet UIButton *btn5;
@property (strong, nonatomic)  NSArray *btnArray;
@property (strong, nonatomic)  NSArray *centerArray;
@end
@implementation SegView
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self loadView];
        self.btnArray = @[self.btn1,self.btn2,self.btn3,self.btn4,self.btn5];
        self.centerArray = @[self.lineCenter1,self.lineCenter2,self.lineCenter3,self.lineCenter4,self.lineCenter5];
    }
    return self;
}
- (IBAction)actionForBtn:(UIButton *)sender {
    self.lineCenter1.active = NO;
    for (int i = 0 ; i<self.centerArray.count; i++)
    {
        NSLayoutConstraint *center = self.centerArray[i];
        UIButton *btn = self.btnArray[i];
        center.active = sender.tag == btn.tag;
        btn.selected = sender.tag == btn.tag;
        if (sender.tag == btn.tag) {
            if (self.block) {
                self.block(sender.currentTitle);
            }
        }
    }
}
- (void)setTitles:(NSArray *)titles{
    _titles = titles;
    for (int i = 0 ; i<_titles.count; i++)
    {
        NSString *t = _titles[i];
        UIButton *btn = self.btnArray[i];
        [btn setTitle:LS(t) forState:UIControlStateNormal];
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}
@end
